/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl3.gui;

import java.util.HashSet;
import javax.swing.table.AbstractTableModel;
import pl3.SyntacticAnalisysTable;

/**
 *
 * @author Luis Alberto Pérez García <luixal@gmail.com>
 */
public class SATTableModel extends AbstractTableModel {

    private SyntacticAnalisysTable sat;
    private String[] columnNames;
    private String[] rowNames;
    
    public SATTableModel(SyntacticAnalisysTable sat) {
        this.sat = new SyntacticAnalisysTable(sat);
        // setting column names:
        HashSet<String> terminals = this.sat.getTerminals();
        this.columnNames = new String[terminals.size()+1];
        this.columnNames[0] = "";
        Integer counter = 1;
        for (String t:terminals) {
            this.columnNames[counter] = t;
            counter++;
        }
        // setting row names:
        HashSet<String> nonTerminals = this.sat.getNonTerminals();
        this.rowNames = new String[nonTerminals.size()];
        counter = 0;
        for (String t:nonTerminals) {
            this.rowNames[counter] = t;
            counter++;
        }
    }

    public int getRowCount() {
        return this.rowNames.length;
    }

    public int getColumnCount() {
        return this.columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return this.columnNames[column];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return this.rowNames[rowIndex];
        } else {
            String x = this.rowNames[rowIndex];
            String y = this.columnNames[columnIndex];
            String result = this.sat.get(x, y);
            if (result.equals("ERROR")) return "--";
            return result;
        }
    }

}
